package com.atguigu.crud.controller;


import com.atguigu.crud.bean.Employee;
import com.atguigu.crud.bean.Msg;
import com.atguigu.crud.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
     * 处理员工CRUD请求
     */

    @Controller
    public class EmployeeController {
        @Autowired
        EmployeeService employeeService;

    /**
     * 员工删除,单个、批量二合一
     * 批量删除，1-2-3
     * 单个删除 1
     * @param id
     * @return
     */

    @ResponseBody
        @RequestMapping(value = "/emp/{id}",method =RequestMethod.DELETE)
        public Msg deleteEmpById(@PathVariable("id") String ids){

        if(ids.contains("-")){
            //批量删除
            List<Integer> del_ids=new ArrayList<>();
            String[] str_ids = ids.split("-");
            //组装id的集合
            for(String string:str_ids){
                del_ids.add(Integer.parseInt(string));
            }
                employeeService.deleteBatch(del_ids);

        }else {
            //单个删除
           Integer id= Integer.parseInt(ids);
            employeeService.deleteEmp(id);
        }

            return Msg.success();
        }

    /**
     * 员工更新方法
      * @param employee
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/emp/{empId}",method = RequestMethod.PUT)
    public  Msg saveEmp(Employee employee){
        employeeService.updateEmp(employee);

        return Msg.success();
    }
    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @RequestMapping(value = "/emp/{id}",method = RequestMethod.GET)
        @ResponseBody
        public Msg getEmp(@PathVariable("id") Integer id){
            Employee employee=employeeService.getEmp(id);
            return Msg.success().add("emp",employee);
        }

    /**
     * 检查用户名是否可用
     * @param empName
     * @return
     */
    @ResponseBody
        @RequestMapping("/checkuser")
        public Msg checkuse(@RequestParam("empName") String empName){
        boolean b=  employeeService.checkUser(empName);
        if (b) {
            return Msg.success();
        }else {
            return Msg.fail();
        }
        }

    /**
     * 员工保存
     * @return
     */
    @RequestMapping(value = "/emp",method = RequestMethod.POST)
    @ResponseBody
    public Msg savaEmp(Employee employee){
    employeeService.savaEmp(employee);
        return Msg.success();
        }
        /**
         * ResponseBody要能工作，要导入jackson包
         * @param pn
         * @param model
         * @return
         */
        @RequestMapping("/emps")
        @ResponseBody
        public Msg getEmpsWithJson(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
            //非分页查询
            //引入PageHelper分页插件
            //在查询之前只需要调用分页插件的方法PageHelper.startPage(),传入页码,及每页显示数
            PageHelper.startPage(pn, 10);
            //startPage后面紧跟的这个查询就是一个分页查询
            List<Employee> emps= employeeService.getAll();
            //使用pageInfo包装查询后的结果，只需要将pageinfo交给页面
            //封装了详细的分页信息，包括我们查询出来的数据
            PageInfo page=new PageInfo(emps,5);
            //navigatePages表示导航页码连续显示5页
            //给Model中保存的数据都会带给页面，会放在请求头
            model.addAttribute("pageInfo",page);
            System.out.println(page);
            return Msg.success().add("pageInfo",page);
        }

    }


